package com.usian.cars.controller;


import com.usian.cars.service.ICarsService;
import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.entity.Cars;
import com.usian.vo.CarsVo;
import com.usian.vo.CarsVo2;
import com.usian.vo.CarsReserveVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/cars/cars")
public class CarsController {

    @Autowired
    private ICarsService carsService;

    @Operation(summary = "根据汽车id查询汽车详情")
    @GetMapping("/getCarByCarId/{carId}")
    public Result<CarsVo> getCarByCarId(@PathVariable Integer carId) {
        return Result.build(carsService.getByIdCarVo(carId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据品牌id查询汽车详情")
    @GetMapping("/getCarAllByBrandId/{BrandId}")
    public Result<List<CarsVo>> getCarAllByBrandId(@PathVariable Integer BrandId) {
        return Result.build(carsService.getCarAllByBrandId(BrandId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据汽车id查询汽车")
    @GetMapping("/getCarsById/{carId}")
//    http://localhost:10086/api/cars/cars/getCarsById/1
    public Result<Cars> getCarsById(@PathVariable Integer carId) {
        return Result.build(carsService.getById(carId), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "筛选查询车辆信息")
    @GetMapping("/findScreen/{brand}/{encodedValue}/{character}")
     public Result<List<CarsReserveVo>> findScreen(@PathVariable(required = false) String brand,
                                                  @PathVariable(required = false) String encodedValue,
                                                  @PathVariable(required = false) String character) throws UnsupportedEncodingException {
        String decodedBrand = URLDecoder.decode(brand, StandardCharsets.UTF_8.toString());
        String decodedTexing = URLDecoder.decode(character, StandardCharsets.UTF_8.toString());
        String decodedPrice = URLDecoder.decode(String.valueOf(encodedValue), StandardCharsets.UTF_8.toString());
        System.out.println("decodedBrand:"+decodedBrand);
        System.out.println("decodedTexing:"+decodedTexing);
        System.out.println("decodedPrice:"+decodedPrice);
        return Result.build(carsService.findScreen(brand, Integer.valueOf(encodedValue),character), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询汽车详情")
    @GetMapping("/getCarAll")
    public Result<List<CarsVo>> getCarAll() {
        return Result.build(carsService.findAll(), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改汽车")
    @PutMapping("/updateCar")
    public Result<Boolean> updateCar(@RequestBody CarsVo cars){
        return Result.build( carsService.updateCar(cars), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "搜索页面根据汽车名称模糊查询汽车信息")
    @PostMapping("/getCarsByCarModel")
    public Result<List<CarsVo>> getCarsByCarModel(@RequestBody Cars cars) {
        return Result.build(carsService.getCarsByCarModel(cars), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改全部车辆收藏")
    @PutMapping("/updateFavorites")
    public Result<Boolean> updateFavorites(){
        return Result.build(carsService.updateFavorites(), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改车辆收藏")
    @PutMapping("/updateFavoritesAll/{carId}")
    public Result<Boolean> updateFavoritesAll(@PathVariable Integer carId){
        return Result.build(carsService.updateFavoritesAll(carId), ResultCodeEnum.SUCCESS);
    }
}
